package util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class IO {
    private static final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    static PrintStream printStream = new PrintStream(byteArrayOutputStream);
    static MultiOutputStream multiOutputStream = new MultiOutputStream(new OutputStream[]{System.out, printStream});
    static Scanner scanner;

    static {
        System.setOut(new PrintStream(multiOutputStream));
        scanner = new Scanner(System.in);
    }

    public static String getOutContent() {
        return byteArrayOutputStream.toString();
    }

    public static void output(String str) {
        System.out.print(str);
    }


    public static void flush() {
        byteArrayOutputStream.reset();
    }

    public static String readLine() {
        return scanner.nextLine();
    }

    static class MultiOutputStream extends OutputStream {
        OutputStream[] outputStreams;

        public MultiOutputStream(OutputStream[] outputStreams) {
            this.outputStreams = outputStreams;
        }

        @Override
        public void write(int b) throws IOException {
            for (OutputStream outputStream : outputStreams) {
                outputStream.write(b);
            }
        }
    }
}
